package com.powernode.controller;

import com.github.pagehelper.PageInfo;
import com.powernode.WebUtils.MD5Utils;
import com.powernode.WebUtils.WebMvcUtils;
import com.powernode.commons.Constants;
import com.powernode.commons.Result;
import com.powernode.domain.User;
import com.powernode.dto.UserDto;
import com.powernode.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 分页查询数据
     */
    @RequestMapping("list.do")
    public Result list(UserDto userDto){
        System.out.println(userDto.getUsername());
        System.out.println(userDto.getUsername());
        System.out.println(userDto.getDeleted());
        PageInfo<User> pageInfo=userService.queryUserForPage(userDto);
        return new Result(0,"查询成功",pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 设置默认密码
     * @param user
     * @return
     */
    @PostMapping("addUser.do")
    public Result addUser(User user){
        try{
            //设置默认密码
            user.setPassword(MD5Utils.md5(Constants.USER_DEFAULT_PWD));

            //设置在职
            user.setDeleted(Constants.DELETED_1);

            //设置创建时间
            user.setCreateTime(new Date());

            //保存
            this.userService.insertSelective(user);
            return Result.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    /**
     * 修改一条数据
     * @param user
     * @return
     */
    @RequestMapping("updateUser.do")
    public Result updateUser(User user){
        try{
            //保存
            this.userService.updateByPrimaryKeySelective(user);
            return Result.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    /**
     * 设置离职
     * @param id
     * @return
     */
    @RequestMapping("quit.do")
    public Result quit(Integer id){
        try{
            //保存
            User user=new User();
            user.setId(id);
            user.setDeleted(Constants.DELETED_2);
            this.userService.updateByPrimaryKeySelective(user);
            return Result.OPT_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return Result.OPT_ERROR;
        }
    }

    /**
     * 重置密码
     * @param id
     * @return
     */
    @RequestMapping("resetPwd.do")
    public Result resetPwd(Integer id){
        try{
            //保存
            User user=new User();
            user.setId(id);
            user.setPassword(MD5Utils.md5(Constants.USER_DEFAULT_PWD));
            this.userService.updateByPrimaryKeySelective(user);
            return Result.OPT_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return Result.OPT_ERROR;
        }
    }

    /**
     * 查询所有的在职的业务员
     */
    @GetMapping("queryAllAvailableUser.do")
    public Result queryAllAvailableUser(){
        List<User> userList=this.userService.queryAllAvailableUser();
        return new Result(200,"查询成功",0L,userList);
    }

    /**
     * 验证旧密码
     */
    @PostMapping("checkOl   dPwd.do")
    public Result checkOldPwd(String oldPwd){
        Integer currentUserId = WebMvcUtils.getCurrentUser().getId();
        //根据id查询数据库里面的用户数据
        User user=this.userService.selectByPrimaryKey(currentUserId);
        if(user==null){
            return new Result(-1,"用户不存在");
        }
        if(MD5Utils.md5(oldPwd).equals(user.getPassword())){
            return new Result(200,"密码匹配正确");
        }else{
            return new Result(-1,"密码不正确");
        }
    }

    /**
     * 修改密码
     */
    @PostMapping("updatePwd.do")
    public Result updatePwd(String newPwd){
        //得到当前用户
        Integer currentUserId = WebMvcUtils.getCurrentUser().getId();
        //构造用户对象
        User user=new User();
        user.setId(currentUserId);
        user.setPassword(MD5Utils.md5(newPwd));
        //修改
        try {
            this.userService.updateByPrimaryKeySelective(user);
            return Result.OPT_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return Result.OPT_ERROR;
        }
    }
}
